#include "APP_MainWork.h"
#include <stdio.h>
#include "adc.h"
#include "APP_KEY.h"
#include "APP_Motor.h"
#include "APP_Output.h"
#include "APP_COM.h"


uint8_t  MainWork_Start = STOP_WORK;
uint8_t  MainWork_Process = MAINWORK_READY;
uint16_t MainWork_Cnt = 0;

uint16_t MainWork_Strengh = 7;
uint16_t MainWork_Dose = 5;

uint16_t MainWork_AutoReady = STOP_WORK;
uint16_t MainWork_NanoReady = STOP_WORK;

uint16_t MainWork_PressValue = 0;

uint16_t MainWork_MotorWork = PUSHMOTOR_STOP_CMD;		//0x0A:Stop		0x0C:back up	0x0D:forward
uint8_t  MainWork_PushMotorLimit = 0;


extern uint16_t Motor_2_Step;


void MainWork_Motor(void)
{
	if(MainWork_Start == STOP_WORK)
	{
		if(MainWork_MotorWork == PUSHMOTOR_BACKUP_CMD)
		{
			if(MainWork_PushMotorLimit != PUSHMOTER_BACKUP_LIMIT)
				PUSHMOTOR_BACKUP();
			else
				MainWork_MotorWork = PUSHMOTOR_STOP_CMD;
		}
		else if(MainWork_MotorWork == PUSHMOTOR_FORWARD_CMD)
		{
			//PUSH MOTOR
			//1:	1100
			//2:	2100
			//10:	10500
//			for(uint16_t i=0; i<(MainWork_Dose*1000+200); i++)
				PUSHMOTOR_FORWARD(PUSHMOTOR_SPEED_FAST);
			//STRENGH MOTOR
//			for(uint16_t i=0; i<(MainWork_Dose*1300); i++)
//				PUSHMOTOR_FORWARD();
//			MainWork_MotorWork = PUSHMOTOR_STOP_CMD;
		}
		else 
			PUSHMOTOR_STOP();
	}
	else
	{
		MainWork_MotorWork = PUSHMOTOR_STOP_CMD;
	}
}

void MainWork_StartWork(void)
{
	uint8_t KEY_Scan = 0;
	static uint8_t NANO_STOP = 0;
	
	if(MainWork_Start == START_WORK)
	{
		if(MainWork_AutoReady == READY_WORK)
		{
			switch(MainWork_Process)
			{
				case MAINWORK_READY:
						printf("Now Process if MAINWORK_READY \r\n");
						MainWork_Process = MAINWORK_INHALE;
					break;
				
				case MAINWORK_INHALE:
						printf("Now Process if MAINWORK_INHALE \r\n");
						OUTPUT_SOLE_OFF();
						OUTPUT_PUMP_ON();
						MainWork_PressValue = ADC_GetAveValue();
						printf("MainWork_PressValue: %4d \r\n", MainWork_PressValue);
						if(MainWork_PressValue >= PRESSVALUE_LIMIT)
						{
							OUTPUT_PUMP_OFF();
							MainWork_Process = MAINWORK_PUSH_PIN;
						}
					break;
						
				case MAINWORK_PUSH_PIN:
						printf("Now Process if MAINWORK_PUSH_PIN \r\n");
						if(MainWork_Strengh > 1)
						{
							for(uint16_t i=0; i<((MainWork_Strengh-1)*1300); i++)
							{
								SOLEMOTOR_FORWARD(SOLEMOTOR_SPEED_SLOW);
							}
						}
						SOLEMOTOR_STOP();
						MainWork_Process = MAINWORK_PUSH_LIQUID;
					break;
						
				case MAINWORK_PUSH_LIQUID:
						printf("Now Process if MAINWORK_PUSH_LIQUID \r\n");
						for(uint16_t i=0; i<(MainWork_Dose*1000+200); i++)
						{
							PUSHMOTOR_FORWARD(PUSHMOTOR_SPEED_SLOW);
							
							if((i%400) == 0)
							{
								MainWork_PressValue = ADC_GetAveValue();
								APP_KEY_Scan(&KEY_Scan);
								if(MainWork_PressValue < PRESSVALUE_LOW_LIMIT
									|| (KEY_Scan & KEY_LIMIT) == KEY_LIMIT)
									break;
							}
						}
						PUSHMOTOR_STOP();
						MainWork_Process = MAINWORK_NARROWING;
					break;
						
				case MAINWORK_NARROWING:
						printf("Now Process if MAINWORK_NARROWING \r\n");
						HAL_Delay(500);
						if(MainWork_Strengh > 1)
						{
//							for(uint16_t i=0; i<((MainWork_Strengh+1)*1300); i++)
							for(uint16_t i=0; i<(MainWork_Strengh*1300); i++)	//(MainWork_Strengh-1)
							{
								SOLEMOTOR_BACKUP(SOLEMOTOR_SPEED_SLOW);
							}
						}
						SOLEMOTOR_STOP();
						MainWork_Process = MAINWORK_DEFLATE;
					break;
						
				case MAINWORK_DEFLATE:
						printf("Now Process if MAINWORK_DEFLATE \r\n");
						OUTPUT_SOLE_ON();
						HAL_Delay(500);
						OUTPUT_SOLE_OFF();
						MainWork_Process = MAINWORK_OVER;
					break;
						
				case MAINWORK_OVER:
						printf("Now Process if MAINWORK_OVER \r\n");
						APP_SendStopWork();
						MainWork_Start = STOP_WORK;
						MainWork_Process = MAINWORK_READY;
					break;
				
				default:break;
			}
		}
		else if(MainWork_NanoReady == READY_WORK)
		{
			switch(MainWork_Process)
			{
				case MAINWORK_READY:
						printf("Now Process if MAINWORK_READY \r\n");
						MainWork_Process = MAINWORK_PUSH_PIN;
					break;
						
				case MAINWORK_PUSH_PIN:
						printf("Now Process if MAINWORK_PUSH_PIN \r\n");
				
						for(uint16_t i=0; i<9000; i++)
						{
							SOLEMOTOR_FORWARD(SOLEMOTOR_SPEED_FAST);
							if(NANO_STOP == 0)
							{
								APP_KEY_Scan(&KEY_Scan);
								if((KEY_Scan & KEY4) == KEY4)
								{
									printf("KEY4 !! \r\n");
									NANO_STOP = 1;
								}
							}
						}
						
						SOLEMOTOR_STOP();
						MainWork_Process = MAINWORK_NARROWING;
					break;
						
				case MAINWORK_NARROWING:
						printf("Now Process if MAINWORK_NARROWING \r\n");
				
						for(uint16_t i=0; i<9000; i++)
						{
							SOLEMOTOR_BACKUP(SOLEMOTOR_SPEED_FAST);
							if(NANO_STOP == 0)
							{
								APP_KEY_Scan(&KEY_Scan);
								if((KEY_Scan & KEY4) == KEY4)
								{
									printf("KEY4 !! \r\n");
									NANO_STOP = 1;
								}
							}
						}
						
						SOLEMOTOR_STOP();
						if(NANO_STOP == 1)
							MainWork_Process = MAINWORK_OVER;
						else
							MainWork_Process = MAINWORK_PUSH_PIN;
					break;
						
				case MAINWORK_OVER:
						printf("Now Process if MAINWORK_OVER \r\n");
						for(uint16_t i=0; i<1000; i++)
						{
							SOLEMOTOR_BACKUP(SOLEMOTOR_SPEED_FAST);
						}
						SOLEMOTOR_STOP();
						APP_SendStopWork();
						NANO_STOP = 0;
						MainWork_Start = STOP_WORK;
						MainWork_Process = MAINWORK_READY;
					break;
				
				default:break;
			}
		}
	}
}

void MainWork_StopWork(void)
{
	if(MainWork_Start == STOP_WORK)
	{
		if(MainWork_AutoReady == READY_WORK)
		{
			switch(MainWork_Process)
			{
				case MAINWORK_READY:
					break;
				
				case MAINWORK_INHALE:
						printf("Now Process if MAINWORK_INHALE \r\n");
						OUTPUT_PUMP_OFF();
						OUTPUT_SOLE_ON();
						HAL_Delay(500);
						OUTPUT_SOLE_OFF();
						MainWork_Process = MAINWORK_OVER;
					break;
						
				case MAINWORK_PUSH_PIN:
						printf("Now Process if MAINWORK_PUSH_PIN \r\n");
						HAL_Delay(500);
						if(MainWork_Strengh > 1)
						{
							for(uint16_t i=0; i<(MainWork_Strengh*1300); i++)	//(MainWork_Strengh-1)
							{
								SOLEMOTOR_BACKUP(SOLEMOTOR_SPEED_SLOW);
							}
						}
						SOLEMOTOR_STOP();
						MainWork_Process = MAINWORK_INHALE;
					break;
						
				case MAINWORK_PUSH_LIQUID:
						PUSHMOTOR_STOP();
						MainWork_Process = MAINWORK_PUSH_PIN;
					break;
						
				case MAINWORK_NARROWING:
					break;
						
				case MAINWORK_DEFLATE:
					break;
						
				case MAINWORK_OVER:
						printf("Now Process if MAINWORK_OVER \r\n");
						MainWork_Process = MAINWORK_READY;
					break;
				
				default:break;
			}
		}
		else if(MainWork_NanoReady == READY_WORK)
		{
		}
	}
}

void APP_MainWork(void)
{
	if(MainWork_Cnt >= 1000)
	{
		MainWork_PressValue = ADC_GetAveValue();
		printf("MainWork_PressValue: %4d \r\n", MainWork_PressValue);
		printf("MainWork_PushMotorLimit: 0x%02X \r\n", MainWork_PushMotorLimit);
		
		MainWork_Cnt = 0;
	}
	
	MainWork_Motor();
	MainWork_StartWork();
	MainWork_StopWork();
}

